package com.doublefish.doublefishrobot.ui.home.widget;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.doublefish.doublefishrobot.R;

/**
 * 作者:dugaofeng on 2024/10/18 16:33
 *
 * 仿写 iOS的  UISegmentControl
 *
 * 整个控件分为3层，从下到上依次为
 * 背景view 用于显示灰色的背景
 * 滑块view 用于显示白色的滑块 和 滑动的动画
 * 文件层 两个TextView  分为显示 '固定值' '范围值'
 */
public class ModeValueRangeSegmentView extends FrameLayout {

    private Context mContext;

    private LinearLayout sliderLL;  //滑块
    private TextView leftTv;
    private TextView rightTv;
    private int index = 0;

    private OnModeValueRangeSegmentViewItemChangedListner onModeValueRangeSegmentViewItemChangedListner;

    public ModeValueRangeSegmentView(@NonNull Context context) {
        this(context, null);
    }

    public ModeValueRangeSegmentView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ModeValueRangeSegmentView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initView();
        initListner();
    }

    public void setOnModeValueRangeSegmentViewItemChangedListner(OnModeValueRangeSegmentViewItemChangedListner onModeValueRangeSegmentViewItemChangedListner) {
        this.onModeValueRangeSegmentViewItemChangedListner = onModeValueRangeSegmentViewItemChangedListner;
    }

    private void initView() {
        inflate(getContext(), R.layout.view_mode_value_range_segment, this);
        sliderLL = findViewById(R.id.ll_slider);
        leftTv = findViewById(R.id.tv_left);
        rightTv = findViewById(R.id.tv_right);
    }

    private void initListner() {
        leftTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index == 0) {
                    return;
                }
                index = 0;
                sliderAnimate(false, true);
                if (onModeValueRangeSegmentViewItemChangedListner != null) {
                    onModeValueRangeSegmentViewItemChangedListner.onModeValueRangeSegmentViewItemChanged(0);
                }
            }
        });

        rightTv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if (index == 1) {
                    return;
                }
                index = 1;
                sliderAnimate(true, true);
                if (onModeValueRangeSegmentViewItemChangedListner != null) {
                    onModeValueRangeSegmentViewItemChangedListner.onModeValueRangeSegmentViewItemChanged(1);
                }
            }
        });
    }

    public void setIndex(int index) {
        this.index = index;

        sliderAnimate(index == 1, false);
    }

    private void sliderAnimate(Boolean right, boolean animate) {
        float fromValue;
        float toValue;
        if (right) {
            fromValue = 0;
            toValue = dip2px(50);
            leftTv.setTypeface(Typeface.DEFAULT);
            rightTv.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            fromValue = dip2px(50);
            toValue = 0;
            leftTv.setTypeface(Typeface.DEFAULT_BOLD);
            rightTv.setTypeface(Typeface.DEFAULT);
        }
        long duration = 1;
        if (animate) {
            duration = 250;
        }
        PropertyValuesHolder translationX = PropertyValuesHolder.ofFloat("translationX", fromValue ,toValue);
        ObjectAnimator.ofPropertyValuesHolder(sliderLL, translationX).setDuration(duration).start();
    }

    private int dip2px(float dpValue) {
        final float scale = getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public interface OnModeValueRangeSegmentViewItemChangedListner {
        void onModeValueRangeSegmentViewItemChanged(int index);
    }
}
